package cn.gx.kevin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.gx.kevin.dao.IRoleDao;
import cn.gx.kevin.dao.IRoleFuncDao;
import cn.gx.kevin.dao.IUserRoleDao;
import cn.gx.kevin.domain.Role;
import cn.gx.kevin.domain.RoleMenu;
import cn.gx.kevin.service.IRoleService;
import cn.gx.kevin.dao.IBaseDao;
import cn.gx.kevin.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Generate by Kevin's CodeBuilder
 **/
@Service
public class RoleService extends BaseService<Role, String> implements IRoleService {

    private static final long serialVersionUID = -1L;

    private IRoleDao roleDao;

    @Autowired
    IRoleFuncDao roleFuncDao;

    @Autowired
    IUserRoleDao userRoleDao;

    @Resource(name = "roleDao")
    @Override
    public void setDao(IBaseDao<Role, String> dao) {
        this.dao = dao;
        roleDao = (IRoleDao) dao;
    }

    @Override
    public JSONArray getAvailabelRole() throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("roleStatus", 1);
        List<Role> roles = this.selectParam(params);
        JSONArray rolesJson = new JSONArray();
        for (Role r : roles) {
            JSONObject jObj = new JSONObject();
            rolesJson.add(jObj);
            jObj.put("id", r.getId());
            jObj.put("roleName", r.getRoleName());
        }
        return rolesJson;
    }

    @Override
    public List<RoleMenu> selectMenusByRole(Long roleId) throws Exception {
        return roleDao.selectMenusByRole(roleId);
    }


    @Override
    public int deleteBatch(List<String> list) throws Exception {
        for (String role : list) {
            roleDao.deleteRoleMenu(Long.valueOf(role));
            Map<String, Object> prs = new HashMap<>();
            prs.put("roleId", role);
            roleFuncDao.deleteParam(prs);
            userRoleDao.deleteParam(prs);
        }
        return super.deleteBatch(list);
    }
}
